package cn.iocoder.yudao.module.erp.controller.admin.priaccount;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.priaccount.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.priaccount.PriAccountDO;
import cn.iocoder.yudao.module.erp.service.priaccount.PriAccountService;

@Tag(name = "管理后台 - ERP 一级库台账")
@RestController
@RequestMapping("/erp/pri-account")
@Validated
public class PriAccountController {

    @Resource
    private PriAccountService priAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 一级库台账")
    @PreAuthorize("@ss.hasPermission('erp:pri-account:create')")
    public CommonResult<Long> createPriAccount(@Valid @RequestBody PriAccountSaveReqVO createReqVO) {
        return success(priAccountService.createPriAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 一级库台账")
    @PreAuthorize("@ss.hasPermission('erp:pri-account:update')")
    public CommonResult<Boolean> updatePriAccount(@Valid @RequestBody PriAccountSaveReqVO updateReqVO) {
        priAccountService.updatePriAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 一级库台账")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:pri-account:delete')")
    public CommonResult<Boolean> deletePriAccount(@RequestParam("id") Long id) {
        priAccountService.deletePriAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 一级库台账")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pri-account:query')")
    public CommonResult<PriAccountRespVO> getPriAccount(@RequestParam("id") Long id) {
        PriAccountDO priAccount = priAccountService.getPriAccount(id);
        return success(BeanUtils.toBean(priAccount, PriAccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 一级库台账分页")
    @PreAuthorize("@ss.hasPermission('erp:pri-account:query')")
    public CommonResult<PageResult<PriAccountRespVO>> getPriAccountPage(@Valid PriAccountPageReqVO pageReqVO) {
        PageResult<PriAccountDO> pageResult = priAccountService.getPriAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PriAccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 一级库台账 Excel")
    @PreAuthorize("@ss.hasPermission('erp:pri-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPriAccountExcel(@Valid PriAccountPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PriAccountDO> list = priAccountService.getPriAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 一级库台账.xls", "数据", PriAccountRespVO.class,
                BeanUtils.toBean(list, PriAccountRespVO.class));
    }

}